package com.sunxueqi.common.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sunxueqi.project.tool.vo.BeanField;
import com.sunxueqi.project.tool.vo.GenerateInput;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;

public class TemplateUtil {
	
	/**
	 * 获取模板文件内容
	 * @param fileName
	 * @return
	 */
	public static String getTemplate(String fileName) {
		FileReader fileReader = new FileReader("generate/" + fileName);
		return fileReader.readString();
	}
	
	/**
	 * 生成java对象代码
	 * @param input
	 */
	public static void saveJava(GenerateInput input) {
		String path = input.getPath();
		String beanPackageName = input.getBeanPackageName();
		String beanName = input.getBeanName();
		//String daoPackageName = input.getDaoPackageName();
		//String daoName = input.getDaoName();
		//String controllerPackageName = input.getControllerPackageName();
		//String controllerName = input.getControllerName();
		
		List<BeanField> list = input.getTableDetail();
		List<String> beanFieldName = new ArrayList<String>();
		List<String> beanFieldType = new ArrayList<String>();
		List<String> beanFieldDefaultValue = new ArrayList<String>();
		for (BeanField beanField: list) {
			if(beanField.getName() != null) beanFieldName.add(beanField.getName());
			if(beanField.getType() != null) beanFieldType.add(beanField.getType());
			if(beanField.getColumnDefault() != null) beanFieldDefaultValue.add(beanField.getColumnDefault());
		}
		
		String content = getTemplate("java.txt");
		
		content = content.replace("{beanPackageName}", beanPackageName);
		
		String imports = "";
		if (beanFieldType.contains(BigDecimal.class.getSimpleName())) {
			imports += "import " + BigDecimal.class.getName() + ";";
		}
		if (beanFieldType.contains(Date.class.getSimpleName())) {
			imports += "import " + Date.class.getName() + ";";
		}
		
		content = content.replace("{import}", imports);
		content = content.replace("{beanName}", beanName);
		
		String fields = getFields(beanFieldName, beanFieldType, beanFieldDefaultValue);
		String getAndSet = getAndSet(beanFieldName, beanFieldType);
		
		content = content.replace("{fields}", fields);
		content = content.replace("{getAndSet}", getAndSet);
		
		FileWriter writer = new FileWriter(path + "/" + beanName + ".java");
		writer.write(content);
	}
	
	private static String getFields(List<String> beanFieldName, List<String> beanFieldType, List<String> beanFieldDefaultValue) {
		StringBuffer buffer = new StringBuffer();
		int size = beanFieldName.size();
		for(int i = 0; i < size; i++) {
			String name = beanFieldName.get(i);
			// 为“id”字段时跳过，不添加id属性
			//if(name.equals("id")) continue;
			String type = beanFieldType.get(i);
			// todo 默认值
			buffer.append("\tprivate ").append(type).append(" ").append(name);
			buffer.append(";\n");
		}
		return buffer.toString();
	}
	
	private static String getAndSet(List<String> beanFieldName, List<String> beanFieldType) {
		StringBuffer buffer = new StringBuffer();
		int size = beanFieldName.size();
		for(int i = 0; i < size; i++) {
			String name = beanFieldName.get(i);
			// 为“id”字段时跳过，不添加id属性的方法
			//if(name.equals("id")) continue;
			String type = beanFieldType.get(i);
			// 生成get()方法
			buffer.append("\tpublic ").append(type).append(" get")
				.append(StrUtil.upperFirst(name)).append("() {\n");
			buffer.append("\t\treturn ").append(name).append(";\n");
			buffer.append("\t}\n");
			// 生成set()方法
			buffer.append("\tpublic ").append("void ").append("set")
				.append(StrUtil.upperFirst(name)).append("(");
			buffer.append(type + " " +name).append(") {\n");
			buffer.append("\t\tthis.").append(name).append(" = ").append(name).append(";\n");
			buffer.append("\t}\n");
		}
		return buffer.toString();
	}
	
	public static void main(String[] args) {
		getTemplate("java.txt");
	}
	
	
}
